﻿namespace net.zxteam.ZXLexer.Runtime.Internal
{
	using System;

	internal class LexerStateScope : IDisposable
	{
		private readonly Action<LexerStateScope, bool> _actionExit;
		private readonly int _initPosition;
		private bool _commited;
		private bool _disposed;

		public LexerStateScope(int initPosition, Action<LexerStateScope, bool> actionExit)
		{
			this._initPosition = initPosition;
			this._actionExit = actionExit;
		}

		public void Dispose()
		{
			if (_disposed) return;

			_disposed = true;

			if (this._actionExit != null)
			{
				this._actionExit(this, _commited);
			}
		}

		public int InitPosition { get { return this._initPosition; } }

		public void Commit() { this._commited = true; }
	}
}
